package com.java.product.controller;

import com.java.param.ProductHotParam;
import com.java.param.ProductIdParam;
import com.java.param.ProductIdsParam;
import com.java.param.ProductPromoParam;
import com.java.product.service.ProductService;
import com.java.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 根据类别名称查询，商品名称
     * 并将商品销售最高的7条数据返回
     * @param productPromoParam 前端类别名称接收类
     * @param result 检验结果
     * @return
     */
    @PostMapping("/promo")
    public R promo(@RequestBody @Validated ProductPromoParam productPromoParam, BindingResult result){
        if(result.hasErrors()){
            return R.fail("参数错误，无法获得类别名称！查询失败！");
        }
        return productService.promo(productPromoParam.getCategoryName());
    }

    /**
     * 多类别热门商品查询
     * @param productHotParam
     * @param result
     * @return
     */
    @PostMapping("/hots")
    public R hots(@RequestBody @Validated ProductHotParam productHotParam,BindingResult result){
        if (result.hasErrors()) {
            return R.fail("参数错误，无法获得类别id集合！");
        }
        return productService.hots(productHotParam);
    }

    /**
     * 查询全部类型信息
     * @return
     */
    @PostMapping("/category/list")
    public R list(){
        return productService.cList();
    }

    /**
     * 根据商品ids，页数，页容量，查询指定商品信息
     * @param productIdsParam
     * @param result
     * @return 商品信息和总数量
     */
    @PostMapping("/bycategory")
    public R byCategory(@RequestBody @Validated ProductIdsParam productIdsParam,BindingResult result){
        if (result.hasErrors()) {
            return R.fail("参数错误，无法查询指定商品信息！");
        }
        return productService.byCategory(productIdsParam);
    }

    /**
     * 根据页码查询全部商品信息
     * @param productIdsParam
     * @param result
     * @return 商品信息和总数量
     */
    @PostMapping("/all")
    public R all(@RequestBody @Validated ProductIdsParam productIdsParam,BindingResult result){
        if (result.hasErrors()) {
            return R.fail("参数错误，无法查询全部商品信息！");
        }
        return productService.byCategory(productIdsParam);
    }

    /**
     * 根据商品id，查询商品详细信息
     * @param productIdParam
     * @param result
     * @return
     */
    @PostMapping("/detail")
    public R detail(@RequestBody @Validated ProductIdParam productIdParam,BindingResult result){
        if (result.hasErrors()) {
            return R.fail("参数错误，无法查询商品详细信息！");
        }
        return productService.detail(productIdParam);
    }

    /**
     * 根据商品id，查询商品全部图片信息
     * @param productIdParam
     * @param result
     * @return
     */
    @PostMapping("/pictures")
    public R pictures(@RequestBody @Validated ProductIdParam productIdParam,BindingResult result){
        if (result.hasErrors()) {
            return R.fail("参数错误，无法查询商品详细图片信息！");
        }
        return productService.pictures(productIdParam);
    }
}
